<?php

	require_once(dirname(__FILE__)."/../core/safeParam.php");

	$error = false;

	if ($location_info[0]['tax'] != "") {
  		$tax_rate = $location_info[0]['tax'];
	} else {
  		$tax_rate = TAX_PERCENT;
	}
		
	require_once "core/cartClass.php";
	require_once "modules/cart.php";
	
	// modules/cart.php
	$shoppingcart = new cartWrapper();	
	
	$del = '';
	
	$cart_object->total();
	$total      = $cart_object->_total;
	$cart_items = $cart_object->getItems();
	
	$smarty->assign('order_total', $total);
	
	$subtotal      = $total;	
	
	/*COMIENZO DE PÁGINA CHECKOUT.PHP*/
	$bigcart = '
	<h1>' . $lang['cart_checkout'] . '</h1>	
	
	<br />

	<div align="center">
		<table width="90%" border="0" align="center" cellspacing="4"  cellpadding="4"  id="bigcart">
			<tr> 				
  				<td align="center">
  					<b><i>Cantidad</i></b>
  				</td>
    			
    			<td align="center">
    				<b><i>Producto</i></b>
    			</td>
    			
    			<td align="center">
    				<b><i>Precio Unitario</i></b>
    			</td>
				
				<td align="center">
					<b><i>Total</i></b>
				</td>
			</tr>';
		
	foreach ($cart_items as $cart_item) {
		$item_price = $cart_item->price;
  		$subtotal = $cart_item->quantity * $item_price;

		/*CONTENIDO WEB*/
  		$bigcart .= "
  		<tr> 				
  			<td align='center' style='font-weight:normal'> 
  				". $cart_item->quantity ."
  			</td>
  			
  			<td align='center' style='font-weight:normal'>
  				". $cart_item->name ."
  			</td>
  			
  			<td align='center' style='font-weight:normal'>
  				". price_format($cart_item->price) ."
  			</td>
  			
  			<td align='center' style='font-weight:normal'>
  				". price_format($subtotal) ."
  			</td>
  		</tr>";  		  		
	} //foreach
	
		
	$_SESSION['sub']             = $total;
	$_SESSION['pickup_discount'] = $tax_rate;
	
	foreach ($cart_items as $cart_itemz) {
		if ($cart_itemz->tax == 0) {
			if ($_SESSION['delivery'] == 0 or $_SESSION['delivery'] == "") {
				$item_price = $cart_itemz->price - $cart_itemz->discount;
    		} else {
      		$item_price = $cart_itemz->price;
    		}
    		$tax_discount .= $item_price * $tax_rate;
  		} 
	} 
	
	$tax = $tax_rate;
	$subtotal = $total;
	
	$_SESSION['tax']      = $tax;
	$_SESSION['subtotal'] = $total;

	$final = $tax + $total;

	if ( (isset($_SESSION['delivery']) && $_SESSION['delivery'] == 1) or (getOption('cb-only-delivery', RID) == 1) ) {
  		$final = $final + $config['deliveryfee'];
	} 
	
	//SUB-TOTAL
	$bigcart .= '
	<tr>
		<td colspan="4">
			<hr>
		</td>
	</tr>
		
	<tr>
		<td colspan="3" style="text-align:right; font-size:1.1em;">
			<b><i>' . $lang['subtotal'] . ':</b></i>  
		</td>
		
		<td align="center" style="font-weight:normal;">
			'.price_format($subtotal) .'
		</td>
	</tr>';
	
	//RECARGO POR ENVIO
	if ($tax_rate != 0) {
		$bigcart .= '
		<tr>
			<td colspan="3" style="text-align:right; font-size:1.1em;">
				<b><i>' . $lang['tax'] . ':</b></i> 
			</td>
			
			<td align="center" style="font-weight:normal;">
				' . price_format($tax) . '
			</td>
		</tr>';
	}
	
	//TOTAL
	$bigcart .= '
	<tr>
		<td colspan="3" style="text-align:right; font-size:1.1em; color:red;">
			<b><i>' . $lang['cart_total'] . ':</b></i>
		</td>
		
		<td align="center" style="font-weight:normal;">
			' . price_format($final) . '
		</td>
	</tr>
	</table>

	<div style="text-align:right; font-weight:normal; font-size:10px; width:90%;">
		' . $lang['sales_final'] . '
	</div>';

	$content = $bigcart;
	//end big cart
	
	if (isset($_GET['checkout']) && $_GET['checkout'] == 2) {
		$content .= '';
		
		// INFORMACION DEL CLIENTE
    	$customer_info .= '
		<table width="950%" border="1" cellspacing="3" cellpadding="3">
 			<tr>
    			<td>' . $lang['ch_name'] . ':</td>
    			<td>' . treatGet($_POST['name']) . ' ' . treatGet($_POST['last_name']) . '</td>
  			</tr>
  				
  			<tr>
    			<td>' . $lang['ch_email'] . ':</td>
    			<td>' . treatGet($_POST['email']) . '</td>
  			</tr>
    			
    		<tr>
    			<td>' . $lang['ch_billing'] . ':</td>
    			<td>
    				' . treatGet($_POST['address']) . ' . ' . treatGet($_POST['address2']) . '
    			</td>
  			</tr>
    			
    		<tr>
    			<td>' . $lang['ch_delivery'] . ':</td>
    			<td>
    				' . treatGet($_POST['delivery_address']) . ' . ' . treatGet($_POST['delivery_address2']) . '
    			</td>
  			</tr>
    			
    		<tr>
    			<td>' . $lang['ch_phone'] . ':</td>
    			<td>' . treatGet($_POST['phone']) . '</td>
  			</tr>
    			
    		<tr>
    			<td>' . $lang['ch_comments'] . ':</td>
    			<td>' . treatGet($_POST['comments']) . '</td>
  			</tr>';
    			
    			
    	$customer_info .= '       			
    		<tr>
    			<td>' . $lang['ch_del'] . '</td>
    			<td>' . treatGet($_POST['delivery']) . '</td>
  			</tr>
        		
        	<tr>
    			<td>' . $lang['ch_payment'] . '</td>
    			<td>' . treatGet($_POST['gateway']) . '</td>
  			</tr>
		</table>';
    
    	$_SESSION['comments'] = $_POST['comments'];   
    	define('ORDER_EMAIL', ADMIN_EMAIL);

    	//INSERT INTO DB
    	$final = $final + $_POST['tip'];
    	$_SESSION['final'] = $final;
    	$_SESSION['abono'] = $_POST['abono'];
    	
    	if (REG == 1) {
    		//insert into order system
      	// insert the order summary
      	$insertsum['uid']        		  = $_SESSION['userid'];
      	
      	$insertsum['lid']        		  = $_SESSION['location'];
      	$insertsum['date']       		  = date("Y-m-d H:i:s");
      	$insertsum['total']      		  = $final;
      	$insertsum['status'] 	 		  = 0;
      	$insertsum['contents']   		  = $bigcart;
      	
      	$insertsum['rid']               = RID;
      	$insertsum['payment']    		  = $_POST['gateway'];
      	$insertsum['delivery']   		  = $_POST['delivery'];
      	$insertsum['address1']          = $_POST['address'];
      	$insertsum['address2']          = $_POST['address2'];
      	
      	$insertsum['delivery_address1'] = $_POST['delivery_address'];
      	$insertsum['delivery_address2'] = $_POST['delivery_address2'];
      	
      	//abono
      	$insertsum['delivery_zip']      = $_POST['abono'];
      	$insertsum['subtotal']   		  = $_SESSION['subtotal'];
      	$insertsum['tax']        		  = $_SESSION['tax'];

      	$insertsum['comments']		     = $_POST['comments'];
      		
      	$database->insert_array('order_sum', $insertsum);
      	
      	$cart_order_id = $database->insertid();
      	$_SESSION['cart_order_id'] = $cart_order_id;
      		
      	// insert the order inventory
      	foreach ($cart_items as $cart_item) {
        		$insertinv['uid']              = $_SESSION['userid'];
        		$insertinv['lid']              = $_SESSION['location'];
        		$insertinv['iid']              = $cart_item->item_id;
	        	$insertinv['oid']              = $cart_order_id;
        		$insertinv['rid']              = RID;
        		$insertinv['item_name']        = $cart_item->name;
        		$insertinv['item_price']       = $cart_item->price - $cart_item->discount;
        		$insertinv['item_options']     = $cart_item->item_options;
        		$insertinv['item_description'] = $cart_item->description;
        		$insertinv['item_quantity']    = $cart_item->quantity;

        		$database->insert_array('order_inv', $insertinv);
      	} 
      	
      } else {
      	//do a soft insert
      	$results_user = $database->select("SELECT * FROM users where email = '" . $_POST['email'] . "'");
      		
      	if ($results_user == false) {
        		$vars['name']      = $_POST['name'];
        		$vars['last_name'] = $_POST['last_name'];
        		$vars['email']     = $_POST['email'];
        		$vars['address']   = $_POST['address'];
        		$vars['address2']  = $_POST['address2'];
        		$vars['city']      = $_POST['city'];
        		$vars['state']     = $_POST['state'];
        		$vars['phone']     = $_POST['phone'];
        		$vars['zip']       = $_POST['zip'];
        		$vars['password']  = $_POST['password'];
        			
        		$database->insert_array('users', $vars);
        			
        		$vars_order['uid']      = mysql_insert_id();
        		$vars_order['lid']      = $_SESSION['location'];
        		$vars_order['total']    = price_format($final);
        		$vars_order['contents'] = $bigcart;
        		
        		$database->insert_array('orders', $vars_order);
      	} else {
        			$vars['uid']             = $results_user[0]['id'];
        			$vars['lid']             = $_SESSION['location'];
        			$vars['total']           = price_format($final);
        			$vars['contents']        = $bigcart;
        			$insertsum['time_ready'] = $_POST['timeready'];
        			
        			$database->insert_array('orders', $vars);
        			$cart_order_id = $database->insertid();
      		}
    		}
    	
    	$cartinfo['cart_order_id'] = $cart_order_id;

		//MANDAR E-MAIL AL DELIVERY    
		//
		//
		//
		//
		//
		//
		//
		//
		//
		//	
    	
    	header("location: sites.php?act=gateway&oid=". $cart_order_id ."&gateway=". $_POST['gateway']. "");    	
    				
	} else {
  		if ($_SESSION['location'] == '') {
    		if ($zip_error == 1) {
      		$_SESSION['delivery'] = 0;
      		
      		$content .= '
      		<p style="color:red;font-weight:bold">
      			We do not deliver to your location, please pick a location to pick up your order.
      		</p>' . locationBox() . '';
    		} else {
      		if (getOption('cb-auto-location', RID) == 1) {
        			$content .= '
        			<h1>Please type your zip code to find the closest location</h1>
					<form action="sites.php?act=checkout" method="post">
						<input type="text" name="zip"> 
						<input type="submit" name="submit_zip" value="Find Location!">
					</form>';
      		
      		} else {
        			$content .= '<h1>Por favor seleccione su Delivery</h1>' . locationBox() . '';
      		}
    		}
  		} else {
    		$check_locations = $database->select("SELECT * FROM locations WHERE rid = '" . RID . "' ");
    		
    		if (count($check_locations) > 1) {
      		$content .= '
      		<div style="margin:5px">
      			' . locationBoxID($_SESSION['location']) . '
      		</div>';
    		} 
    		
    		if (REG == 1) {
      		if ($_SESSION['userid'] == '') {
        			header("Location: sites.php?act=reg&return=sites.php?act=checkout");
      		} else {
        			$content .= $shoppingcart->cartFormReg(0, 0);
      		}
    		} else {
      		if (REG == 1) {
        			$content .= $shoppingcart->cartFormReg(0, 0);
      		} else {
        			$content .= $shoppingcart->cartForm(0, 0);
      		}
    		}
  		}
	}
	
	$smarty->assign('error', $error);
	$smarty->assign('page_content', $content);
	$smarty->assign('content', $content);
?>